package com.zz.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zz.entity.Entry;
import com.zz.entity.Po;
import com.zz.entity.Poreturns;
import com.zz.entity.Product;
import com.zz.entity.Supplier;
import com.zz.entity.Ware;
import com.zz.service.EntryService;
import com.zz.util.KeyUtil;


@RestController
@RequestMapping("entry")
public class EntryController {
	@Resource
	EntryService entryService;
	//查询仓库信息
	@RequestMapping("findAllWare")
	public List<Ware> findAllWare(){
		return entryService.findAllWare();
	}
	//查询供应商信息
	@RequestMapping("findAllSupplier")
	public List<Supplier> findAllSupplier(){
		return entryService.findAllSupplier();
	}
	//查询产品是否为首次入库
	
	@RequestMapping("checkpro")
	public Map checkpro(HttpServletRequest request) {
		String name=request.getParameter("inrkname");
		String type=request.getParameter("sel1");
		String wareid=request.getParameter("wareidin");
		String supplierid=request.getParameter("supplieridin");
		System.out.println(name+type+wareid+supplierid);
		Product p=entryService.findByNameAndTypeAndWareIdAndSupplierId(name, type, wareid, supplierid);
		System.out.println(p);
		boolean entryflag=false;
		if(p!=null) {
			entryflag=false;
		}else{
			entryflag=true;
			System.out.println(entryflag);
		}
		Map map=new HashMap();
		map.put("entryflag", entryflag);
		return map;
	}
	//首次录入
	@RequestMapping("entryFirstPro")
	public Product entryFirstPro(HttpServletRequest request) {
		String name=request.getParameter("inrkname");
		String type=request.getParameter("sel1");
	
		String wareid=request.getParameter("wareidin");
		String supplierid=request.getParameter("supplieridin");
		String locationid=request.getParameter("locationid");
		int num=0;
			Product p2=new Product();
			 p2.setId("SP"+KeyUtil.genUniqueKey());
				p2.setLocation(locationid);
				p2.setName(name);
				p2.setNum(num);
				p2.setType(type);
				p2.setSupplierId(supplierid);
				p2.setWareId(wareid);
			return entryService.saveproduct(p2);
	}
	//入库根据名字查询产品信息
	@RequestMapping("selectproductRKlistByname")
	public List<Object[]> selectproductRKlistByname(HttpServletRequest request){
		String name=request.getParameter("selectname");
		return entryService.selectproductRKlistByname(name);
	}
	//查询产品Byid
	@RequestMapping("findproductById")
	public Optional<Product> findproductById(HttpServletRequest request) {
		String proid=request.getParameter("rkproductid");
		return entryService.findproductById(proid);
	}
	//保存入库单信息
	
	@RequestMapping("saveEntry")
		public Map saveEntry(HttpServletRequest request) {
			String productid=request.getParameter("rkproductid");
			String productname=request.getParameter("rkname");
			String producttype=request.getParameter("rktype");
			String entryprice=request.getParameter("rkpriceid");
			String productnum=request.getParameter("rknumid");
			String productlocation=request.getParameter("rklocationid");
			String wareid=request.getParameter("rkwareidin");
			String supplierid=request.getParameter("rksupplieridin");
			String entrynotes=request.getParameter("rknotesid");
			String entrytype=request.getParameter("intype");
			String poid=request.getParameter("cgid");
			int num=entryService.findproductById(productid).get().getNum();
			Product product=new Product();
			product.setId(productid);
			product.setName(productname);
			product.setType(producttype);
			
			product.setNum((Integer.parseInt(productnum)+num));
			System.out.println("产品库存："+product.getNum());
			product.setLocation(productlocation);
			product.setWareId(wareid);
			product.setSupplierId(supplierid);
			entryService.saveproduct(product);
			
			Entry entry=new Entry();
			entry.setId("RK"+KeyUtil.genUniqueKey());
			entry.setProductId(productid);
			entry.setNotes(entrynotes);
			entry.setSupplierId(supplierid);
			entry.setUpdateNum(Integer.parseInt(productnum));
			entry.setWareId(wareid);
			entry.setType(entrytype);
			entry.setPoId(poid);
			BigDecimal entryPrice=new BigDecimal(entryprice);
			entry.setEntryPrice(entryPrice);
			entryService.saveEntry(entry);
			
			Optional<Po> po=entryService.findPoById(poid);
			
			Po inpo=new Po();
			inpo.setId(po.get().getId());
			inpo.setEntryId(entry.getId());
			inpo.setPoNum(po.get().getPoNum());
			inpo.setProductId(po.get().getProductId());
			inpo.setStatus("已入库");
			entryService.savePolist(inpo);
			Map map=new HashMap();
			map.put("productentity", product);
			map.put("entryentity",entry);
			return map;
		}
	@RequestMapping("saveEntry2")
	public Map saveEntry2(HttpServletRequest request) {
		String productid=request.getParameter("rkproductid");
		String productname=request.getParameter("rkname");
		String producttype=request.getParameter("rktype");
		String entryprice=request.getParameter("rkpriceid");
		String productnum=request.getParameter("rknumid");
		String productlocation=request.getParameter("rklocationid");
		String wareid=request.getParameter("rkwareidin");
		String supplierid=request.getParameter("rksupplieridin");
		String entrynotes=request.getParameter("rknotesid");
		String entrytype=request.getParameter("intype");
		String poid=request.getParameter("cgid");
		int num=entryService.findproductById(productid).get().getNum();
		Product product=new Product();
		product.setId(productid);
		product.setName(productname);
		product.setType(producttype);
		
		product.setNum((Integer.parseInt(productnum)+num));
		System.out.println("产品库存："+product.getNum());
		product.setLocation(productlocation);
		product.setWareId(wareid);
		product.setSupplierId(supplierid);
		entryService.saveproduct(product);
		
		Entry entry=new Entry();
		entry.setId("RK"+KeyUtil.genUniqueKey());
		entry.setProductId(productid);
		entry.setNotes(entrynotes);
		entry.setSupplierId(supplierid);
		entry.setUpdateNum(Integer.parseInt(productnum));
		entry.setWareId(wareid);
		entry.setType(entrytype);
		entry.setPoId(poid);
		BigDecimal entryPrice=new BigDecimal(entryprice);
		entry.setEntryPrice(entryPrice);
		entryService.saveEntry(entry);
		
		
		Map map=new HashMap();
		map.put("productentity", product);
		map.put("entryentity",entry);
		return map;
	}
	
	//对入库单进行分页查询
	@RequestMapping("showAllEntrylist")
	public Page<Object[]> showAllEntrylist(HttpServletRequest request){
		String startpage=request.getParameter("start");
		System.out.println("showAllEntryliststartpage=="+startpage);
		return entryService.showAllEntrylist(startpage, "5");
	}
	//入库单表最近一周查询
	@RequestMapping("showEntrylistBy7day")
	public Page<Object[]> showEntrylistBy7day(HttpServletRequest request){
		String startpage=request.getParameter("start");
		System.out.println("showAllEntryliststartpage=="+startpage);
		return entryService.showEntrylistBy7day(startpage, "5");
	}
	//入库单表最近一周查询
		@RequestMapping("showEntrylistBy30day")
		public Page<Object[]> showEntrylistBy30day(HttpServletRequest request){
			String startpage=request.getParameter("start");
			System.out.println("showAllEntryliststartpage=="+startpage);
			return entryService.showEntrylistBy30day(startpage, "5");
		}
		@RequestMapping("findprofuctIdByPWS30day")
		public Page<Object[]> findprofuctIdByPWS30day(HttpServletRequest request){
			String startpage=request.getParameter("start2");
			String productid=request.getParameter("entryproductid");
			return entryService.findprofuctIdByPWS30day(productid, startpage, "5");
		}
	//入库单表一段时间查询
		@RequestMapping("showEntrylistBytime")
		public Page<Object[]> showEntrylistBytime(HttpServletRequest request){
			String starttime=request.getParameter("selectstarttime");
			String lasttime=request.getParameter("selectlasttime");
			System.out.println("starttime------"+starttime);
			System.out.println("lasttime-------"+lasttime);
			String startpage=request.getParameter("start");
			System.out.println("showAllEntryliststartpage=="+startpage);
			return entryService.showEntrylistBytime(starttime, lasttime, startpage, "5");
		}
		
	//查询采购单信息
		@RequestMapping("showAllPolist")
		public Page<Object[]> showAllPolist(HttpServletRequest request){
			String startpage=request.getParameter("start");
			System.out.println("startpage=="+startpage);
			return entryService.showAllPolist(startpage, "5");
		}
		//查询供应商名字
		@RequestMapping("findsuppliernameByprandware")
		public List<Object[]> findsuppliernameByprandware(HttpServletRequest request){
			String productname=request.getParameter("selectname");
			String warename=request.getParameter("wareidin");
			return entryService.selectproductlistByPrnameandWarename(productname, warename);
		}
		//找到产品Id
		@RequestMapping("findprofuctIdByPWS")
		public List<Object[]> findprofuctIdByPWS(HttpServletRequest request){
			String productname=request.getParameter("selectname");
			String warename=request.getParameter("wareidin");
			String suppliername=request.getParameter("supplieridcg");
			return entryService.findprofuctIdByPWS(productname, warename, suppliername);
		}
		//存储采购单信息
		@RequestMapping("savePolist")
		public Po savePolist(HttpServletRequest request) {
			String productid=request.getParameter("cgproductid");
			String ponum=request.getParameter("cgnumid");
			Po po=new Po();
			po.setId("CG"+KeyUtil.genUniqueKey());
			po.setPoNum(Integer.parseInt(ponum));
			po.setProductId(productid);
			po.setStatus("未入库");
			return entryService.savePolist(po);
		}
		//采购单7天查询
		@RequestMapping("showPolistBy7Day")
		public Page<Object[]> showPolistBy7Day(HttpServletRequest request){
			String startpage=request.getParameter("start");
			return entryService.showPolistBy7Day(startpage, "5");
		}
		@RequestMapping("showPolistBy30Day")
		public Page<Object[]> showPolistBy30Day(HttpServletRequest request){
			String startpage=request.getParameter("start");
			return entryService.showPolistBy30Day(startpage, "5");
		}
		@RequestMapping("showPolistBytime")
		public Page<Object[]> showPolistBytime(HttpServletRequest request){
			String starttime=request.getParameter("selectstarttime");
			String lasttime=request.getParameter("selectlasttime");
			String startpage=request.getParameter("start");
			return entryService.showPolistBytime(starttime, lasttime, startpage, "5");
		}
		//查询采购信息byId
		@RequestMapping("findPoById")
		public Optional<Po> findPoById(HttpServletRequest request) {
			String poid=request.getParameter("cgid");
			return entryService.findPoById(poid);
		}
		@RequestMapping("findPolistById")
		public List findPolistById(HttpServletRequest request) {
			String poid=request.getParameter("cgid");
			return entryService.findPolistById(poid);
		}
		//退货信息存储以及采购单改变
		@RequestMapping("savePoreturns")
		public Map savePoreturns(HttpServletRequest request) {
			String poid=request.getParameter("poid");
			String returnsnotes=request.getParameter("comment");
			String returnnum1=request.getParameter("thnumid");
			Optional<Po> po=entryService.findPoById(poid);
			Po inpo=new Po();
			Poreturns poreturns=new Poreturns();
			poreturns.setId("POT"+KeyUtil.genUniqueKey());
			poreturns.setNotes(returnsnotes);
			poreturns.setPo_id(po.get().getId());
			
			int ponum=po.get().getPoNum();
			int returnnum=Integer.parseInt(returnnum1);
			poreturns.setRetrunsNum(returnnum);
			inpo.setId(po.get().getId());
			inpo.setReturnsId(poreturns.getId());
			inpo.setPoNum(ponum);
			inpo.setProductId(po.get().getProductId());
			if(ponum==returnnum) {
				inpo.setStatus("全部退货");
			}else {
				inpo.setStatus("部分退货");
			}
			
			entryService.savePolist(inpo);
			entryService.savePoreturns(poreturns);
			Map map=new HashMap();
			map.put("PolistTH", inpo);
			map.put("PoreturnsTH",poreturns);
			return map;
			
		}
		//退货信息查询
		@RequestMapping("findPoreturnsbyPoid")
		public List findPoreturnsbyPoid(HttpServletRequest request) {
			String poid=request.getParameter("findpoid");
			return entryService.findPoreturnsbyPoid(poid);
		}
}
